package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.PreventionTypeDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPreventionDAO;

import cn.edu.scau.cmi.hombio.domain.PreventionType;
import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.TaskPrevention;

import cn.edu.scau.cmi.hombio.service.TaskPreventionService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for TaskPrevention entities
 * 
 */

@Controller("TaskPreventionRestController")

public class TaskPreventionRestController {

	/**
	 * DAO injected by Spring that manages PreventionType entities
	 * 
	 */
	@Autowired
	private PreventionTypeDAO preventionTypeDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages TaskPrevention entities
	 * 
	 */
	@Autowired
	private TaskPreventionDAO taskPreventionDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for TaskPrevention entities
	 * 
	 */
	@Autowired
	private TaskPreventionService taskPreventionService;

	/**
	 * View an existing Task entity
	 * 
	 */
	@RequestMapping(value = "/TaskPrevention/{taskprevention_id}/task/{task_id}", method = RequestMethod.GET)
	@ResponseBody
	public Task loadTaskPreventionTask(@PathVariable Integer taskprevention_id, @PathVariable Integer related_task_id) {
		Task task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		return task;
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Save an existing TaskPrevention entity
	* 
	*/
	@RequestMapping(value = "/TaskPrevention", method = RequestMethod.PUT)
	@ResponseBody
	public TaskPrevention saveTaskPrevention(@RequestBody TaskPrevention taskprevention) {
		taskPreventionService.saveTaskPrevention(taskprevention);
		return taskPreventionDAO.findTaskPreventionByPrimaryKey(taskprevention.getId());
	}

	/**
	* Create a new PreventionType entity
	* 
	*/
	@RequestMapping(value = "/TaskPrevention/{taskprevention_id}/preventionType", method = RequestMethod.POST)
	@ResponseBody
	public PreventionType newTaskPreventionPreventionType(@PathVariable Integer taskprevention_id, @RequestBody PreventionType preventiontype) {
		taskPreventionService.saveTaskPreventionPreventionType(taskprevention_id, preventiontype);
		return preventionTypeDAO.findPreventionTypeByPrimaryKey(preventiontype.getId());
	}

	/**
	* Show all TaskPrevention entities
	* 
	*/
	@RequestMapping(value = "/TaskPrevention", method = RequestMethod.GET)
	@ResponseBody
	public List<TaskPrevention> listTaskPreventions() {
		return new java.util.ArrayList<TaskPrevention>(taskPreventionService.loadTaskPreventions());
	}

	/**
	* Delete an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskPrevention/{taskprevention_id}/task/{task_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskPreventionTask(@PathVariable Integer taskprevention_id, @PathVariable Integer related_task_id) {
		taskPreventionService.deleteTaskPreventionTask(taskprevention_id, related_task_id);
	}

	/**
	* Get PreventionType entity by TaskPrevention
	* 
	*/
	@RequestMapping(value = "/TaskPrevention/{taskprevention_id}/preventionType", method = RequestMethod.GET)
	@ResponseBody
	public PreventionType getTaskPreventionPreventionType(@PathVariable Integer taskprevention_id) {
		return taskPreventionDAO.findTaskPreventionByPrimaryKey(taskprevention_id).getPreventionType();
	}

	/**
	* Create a new TaskPrevention entity
	* 
	*/
	@RequestMapping(value = "/TaskPrevention", method = RequestMethod.POST)
	@ResponseBody
	public TaskPrevention newTaskPrevention(@RequestBody TaskPrevention taskprevention) {
		taskPreventionService.saveTaskPrevention(taskprevention);
		return taskPreventionDAO.findTaskPreventionByPrimaryKey(taskprevention.getId());
	}

	/**
	* Select an existing TaskPrevention entity
	* 
	*/
	@RequestMapping(value = "/TaskPrevention/{taskprevention_id}", method = RequestMethod.GET)
	@ResponseBody
	public TaskPrevention loadTaskPrevention(@PathVariable Integer taskprevention_id) {
		return taskPreventionDAO.findTaskPreventionByPrimaryKey(taskprevention_id);
	}

	/**
	* Get Task entity by TaskPrevention
	* 
	*/
	@RequestMapping(value = "/TaskPrevention/{taskprevention_id}/task", method = RequestMethod.GET)
	@ResponseBody
	public Task getTaskPreventionTask(@PathVariable Integer taskprevention_id) {
		return taskPreventionDAO.findTaskPreventionByPrimaryKey(taskprevention_id).getTask();
	}

	/**
	* Create a new Task entity
	* 
	*/
	@RequestMapping(value = "/TaskPrevention/{taskprevention_id}/task", method = RequestMethod.POST)
	@ResponseBody
	public Task newTaskPreventionTask(@PathVariable Integer taskprevention_id, @RequestBody Task task) {
		taskPreventionService.saveTaskPreventionTask(taskprevention_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Save an existing PreventionType entity
	* 
	*/
	@RequestMapping(value = "/TaskPrevention/{taskprevention_id}/preventionType", method = RequestMethod.PUT)
	@ResponseBody
	public PreventionType saveTaskPreventionPreventionType(@PathVariable Integer taskprevention_id, @RequestBody PreventionType preventiontype) {
		taskPreventionService.saveTaskPreventionPreventionType(taskprevention_id, preventiontype);
		return preventionTypeDAO.findPreventionTypeByPrimaryKey(preventiontype.getId());
	}

	/**
	* View an existing PreventionType entity
	* 
	*/
	@RequestMapping(value = "/TaskPrevention/{taskprevention_id}/preventionType/{preventiontype_id}", method = RequestMethod.GET)
	@ResponseBody
	public PreventionType loadTaskPreventionPreventionType(@PathVariable Integer taskprevention_id, @PathVariable Integer related_preventiontype_id) {
		PreventionType preventiontype = preventionTypeDAO.findPreventionTypeByPrimaryKey(related_preventiontype_id, -1, -1);

		return preventiontype;
	}

	/**
	* Delete an existing TaskPrevention entity
	* 
	*/
	@RequestMapping(value = "/TaskPrevention/{taskprevention_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskPrevention(@PathVariable Integer taskprevention_id) {
		TaskPrevention taskprevention = taskPreventionDAO.findTaskPreventionByPrimaryKey(taskprevention_id);
		taskPreventionService.deleteTaskPrevention(taskprevention);
	}

	/**
	* Save an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskPrevention/{taskprevention_id}/task", method = RequestMethod.PUT)
	@ResponseBody
	public Task saveTaskPreventionTask(@PathVariable Integer taskprevention_id, @RequestBody Task task) {
		taskPreventionService.saveTaskPreventionTask(taskprevention_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Delete an existing PreventionType entity
	* 
	*/
	@RequestMapping(value = "/TaskPrevention/{taskprevention_id}/preventionType/{preventiontype_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskPreventionPreventionType(@PathVariable Integer taskprevention_id, @PathVariable Integer related_preventiontype_id) {
		taskPreventionService.deleteTaskPreventionPreventionType(taskprevention_id, related_preventiontype_id);
	}
}